package com.timesheet.server.core.dao;

import com.timesheet.server.core.util.PMManager;

public abstract class BaseDao implements IBaseDao {

	protected final boolean persist(Object entity) {
		//begin();
		try {
			PMManager.persist(entity);
			System.out.println("successfully persists object");
			return true;
		}
		catch (Exception exception) {
			exception.printStackTrace();
			return false;
		}

	}

	/*public final void begin() {
		EntityTransaction transaction = entityManager.getTransaction();
		transaction.begin();
	}*/

	/*private static EntityManager entityManager = EMF.getEntityManager();

	public final EntityManager getEntityManager() {
		return entityManager;
	}

	protected final boolean persist(Object entity) {
		begin();
		entityManager.persist(entity);
		commitAndClose();
		System.out.println("successfully persists object");
		return true;
	}

	public final void begin() {
		EntityTransaction transaction = entityManager.getTransaction();
		transaction.begin();
	}

	public void commitAndClose() {
		entityManager.getTransaction().commit();
		//entityManager.close();

	}

	public <T> T find(Class<T> cls, String id) {
		T t = entityManager.find(cls, id);
		return t;
	}

	public <T> T find(Class<T> cls, int id) {
		T t = entityManager.find(cls, id);
		return t;
	}

	public <T> T find(Class<T> cls, long id) {
		T t = entityManager.find(cls, id);
		return t;
	}

	public List getAll(String query) {

		//begin();

		return entityManager.createQuery(query).getResultList();

	}

	public Query cre1ateQuery1(String query) {

		return entityManager.createQuery(query);
	}

	public List createQuery(String query) {

		return entityManager.createQuery(query).getResultList();
	}

	public List createNativeQueries(String sqlQueries, Class cls) {
		return entityManager.createNativeQuery(sqlQueries, cls).getResultList();
	}

	public List createQuery(String query, String variable, String value) {

		Query createQuery = entityManager.createQuery(query);
		createQuery.setParameter(variable, value);
		return createQuery.getResultList();
	}*/

}